/*
 *name:zhang-lingfan
 *e-mail:1797098277@qq.com
 *qq:1797098277
 *
 *
 *程序说明：实现Vector类。
 *
 *
 */

#include <algorithm>
#include <iostream>


template <typename Object>
class Vector
{
public:
    explicit Vector(int initSize = 0) : theSize{ initSize },
        theCapacity{ initSize + SPARE_CAPACITY }
        ///构造函数，函数初始化并分配空间
    {objects = new Object[theCapacity];}
    Vector(const Vector& rhs) : theSize{ rhs.theSize },
        theCapacity{ rhs.theCapacity }, objects{ nullptr }
        ///拷贝构造函数，将rhs的值赋给所定义的vector类
    {
        objects = new Object[theCapacity];
        for (int k = 0; k < theSize; ++k)
            objects[k] = rhs.objects[k];
    }

    Vector& operator = (const Vector& rhs)
        ///swap
    {
        Vector copy = rhs;
        std::swap(*this, copy);
        return *this;
    }
    ~Vector()///析构函数，释放向量内存
    {
        delete[] objects;
    }
    Vector(Vector&& rhs) :theSize{ rhs.theSize },
        theCapacity{ rhs.theCapacity }, objects{ rhs.objects }
        ///拷贝构造函数，初始化，分配空间为零
    {
        rhs.objects = nullptr;
        rhs.theSize = 0;
        rhs.theCapacity = 0;
    }
    Vector& operator = (Vector&& rhs)///
    {
        std::swap(theSize, rhs.theSize);
        std::swap(theCapacity, rhs.theCapacity);
        std::swap(objects, rhs.objects);

        return *this;
    }
    void resize(int newSize)///定义一个新的size，并分配两倍的newsize的空间
    {
        if (newSize > theCapacity)
            reverse(newSize * 2);
        theSize = newSize;
    }

    void reverse(int newCapacity)///分配一个新的更大的空间（若新空间更小则保持原有的）
    {
        if (newCapacity < theSize)
            return;
        Object* newArray = new Object[newCapacity];
        for (int k = 0; k < theSize; ++k)
            newArray[k] = std::move(objects[k]);
        theCapacity = newCapacity;
        std::swap(objects, newArray);
        delete[] newArray;

        count += 1;
    }

    Object& operator[](int index)///运算符[],返回index位置的元素
    {
        return objects[index];
    }
    const Object& operator[](int index) const///返回index位置的元素，const
    {
        return objects[index];
    }

    bool empty() const///判断是否为空向量
    {
        return size() == 0;
    }

    int size() const///返回theSize
    {
        return theSize;
    }
    int capacity() const///返回theCapacity
    {
        return theCapacity;
    }

    void  push_back(const Object& x)///将空间扩大并且添加x在末尾
    {
        if (theSize == theCapacity)
            reverse(2 * theCapacity + 1);
        objects[theSize++] = x;
    }

    void push_back(Object&& x)//函数重载
    {
        if (theSize == theCapacity)
            reverse(2 * theCapacity + 1);
        objects[theSize++] = std::move(x);
    }

    void pop_back()///删掉一个单位的size
    {
        --theSize;
    }
    const Object& back() const///返回倒数第二个位置所对应的元素
    {
        return objects[theSize - 1];

    }

    typedef Object* iterator;///定义指针名称
    typedef const Object* const_iterator;///定义指针名称

    iterator begin()///返回首位元素的地址
    {
        return &objects[0];
    }
    const_iterator begin() const///begin函数重载，const
    {
        return &objects[0];
    }

    iterator end()///返回末位非空元素的地址
    {
        return &objects[size()];
    }
    const_iterator end() const
    {
        return &objects[size()];
    }///end函数重载，const

    int SPARE_CAPACITY = 16;

    int countout()//返回count值
    {
        return this->count;
    }

    void sparechange(int change)//改变SPARE_CAPACITY的值为change
    {
        this->SPARE_CAPACITY = change;
    }

    int spareout()//返回SPARE_CAPACITY的值
    {
        return this->SPARE_CAPACITY;
    }
private:
    int theSize;
    int theCapacity;
    Object* objects;
    int count = 0;
};


